package com.liull.netty.itcast.nio.filechannel;

import cn.hutool.core.lang.Console;

import java.io.*;
import java.nio.channels.FileChannel;

/**
 * @Author: liull
 * @Description: transferTo实现文件拷贝
 * @Date: Created in 0:19 2021/7/22
 * @Modified By:
 */
public class FileChannelTest {
    public static void main(String[] args) {
        try (FileChannel src = new FileInputStream("F:\\安装包\\系统镜像\\linux\\CentOS-7-x86_64-DVD-1810.iso").getChannel();
                FileChannel tar = new FileOutputStream("bytebuffer_copy.txt").getChannel()) {

            // len表示剩余的大小
            long curTime = System.currentTimeMillis();
            for (long len = src.size(); len > 0;) {
                // 返回实际传输的大小
                len -= src.transferTo(src.size() - len, len, tar);
            }
            Console.log("总共耗时：{}毫秒", System.currentTimeMillis() - curTime);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
